package com.wfc.servlet.user;

import com.wfc.entity.Dish;
import com.wfc.entity.User;
import com.wfc.service.DishService;
import com.wfc.service.UserService;
import com.wfc.service.serviceImpl.DishServiceImpl;
import com.wfc.service.serviceImpl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

//@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("user1");
        String password = req.getParameter("pass");
        User user = new User(username,password);
        HttpSession session = req.getSession();

        //获取用户信息
        UserService userService = new UserServiceImpl();
        User news_user1 = userService.ServiceByUsername(username);

        //获取菜单信息
        DishService dishService = new DishServiceImpl();
        List<Dish> dishByTitle = dishService.getDishByTitle();
        //session存所有菜品集合
        session.setAttribute("dishList",dishByTitle);

        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html; charset=utf-8");
        PrintWriter writer = resp.getWriter();
        String mess="";
        if(password.equals(news_user1.getPassword()) ){
            //session存用户信息
            session.setAttribute("user",news_user1);
            mess="登录成功";
//          resp.sendRedirect(req.getContextPath()+"/index.jsp");
        }else {
            mess="登录失败";
//          resp.sendRedirect(req.getContextPath()+"/Login.jsp");
        }
        writer.println(mess);
        writer.flush();
        writer.close();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        String name=req.getParameter("username");
//        String pass = req.getParameter("password");
//
//        UserService userService = new UserServiceImpl();
//        List<User> newsList = userService.ServiceSelectAll();
//        resp.setContentType("text/html;charset=utf-8");
//        PrintWriter printWriter=resp.getWriter();
//        User user = new User();
//        String mag="1";
//        for (int i=0;i<newsList.size();i++){
//            if (newsList.get(i).getPhone().equals(name)&&newsList.get(i).getPassword().equals(pass)){
//                mag="1";
//                break;
//            }else {
//                mag="0";
//            }
//        }
//        //resp.setCharacterEncoding("text/html;charset=UTF-8");
//        printWriter.println(mag);
//        printWriter.flush();
//        printWriter.close();
    }
}
